package com.ruoyi.warehousemanager.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.warehousemanager.domain.ItemType;
import com.ruoyi.warehousemanager.pojo.ItemVO;
import com.ruoyi.warehousemanager.service.ItemTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.warehousemanager.domain.Item;
import com.ruoyi.warehousemanager.service.ItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 易耗品管理Controller
 *
 * @author fan
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/warehousemanager/item")
public class ItemController extends BaseController
{
    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemTypeService typeService;

    /**
     * 查询易耗品管理列表
     */
    @PreAuthorize("@ss.hasPermi('warehousemanager:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(Item item)
    {
        startPage();
        List<ItemVO> list = itemService.selectItemList(item);
        return getDataTable(list);
    }
    /**
     * 导出易耗品管理列表
     */
    @PreAuthorize("@ss.hasPermi('warehousemanager:item:export')")
    @Log(title = "易耗品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Item item)
    {
        List<ItemVO> list = itemService.selectItemList(item);
        ExcelUtil<ItemVO> util = new ExcelUtil<ItemVO>(ItemVO.class);
        util.exportExcel(response, list, "易耗品管理数据");
    }

    /**
     * 获取易耗品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehousemanager:item:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return success(itemService.selectItemByItemId(itemId));
    }
    /**
     * 获取物品类型列表
     */
    @Anonymous
    @GetMapping("/typeList")
    public AjaxResult getTypeList(){
        return success(typeService.selectItemTypeList(new ItemType()));
    }

    /**
     * 新增易耗品管理
     */
    @PreAuthorize("@ss.hasPermi('warehousemanager:item:add')")
    @Log(title = "易耗品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Item item)
    {
        if(itemService.selectItemList(item).size()>0){
            Item oldItem = itemService.selectItemList(item).get(0);
            item.setItemId(oldItem.getItemId());
            item.setItemNum(oldItem.getItemNum()+ item.getItemNum());
            return toAjax(itemService.updateItem(item));
        }else{
            return toAjax(itemService.insertItem(item));
        }

    }

    /**
     * 修改易耗品管理
     */
    @PreAuthorize("@ss.hasPermi('warehousemanager:item:edit')")
    @Log(title = "易耗品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Item item)
    {
        return toAjax(itemService.updateItem(item));
    }

    /**
     * 删除易耗品管理
     */
    @PreAuthorize("@ss.hasPermi('warehousemanager:item:remove')")
    @Log(title = "易耗品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(itemService.deleteItemByItemIds(itemIds));
    }
}
